package com.example.shop.controller;

import com.example.shop.bean.InfoBean;
import com.example.shop.bean.ProductBean;
import com.example.shop.mapper.InfoMapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/info")
public class InfoController {
    @Autowired
    InfoMapper infoMapper;

    //查询
    @ResponseBody
    @RequestMapping("/list")
    public List<InfoBean> InfoList(){
        List<InfoBean> infoL= infoMapper.infoList();

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        for(InfoBean bean:infoL){
            bean.ftime=sdf.format(bean.ctime);
        }
        return infoL;
    }
    //增加
    @ResponseBody
    @RequestMapping("/add")
    public  String infoAdd(InfoBean bean){
        bean.ctime=new Date();
        try{
            infoMapper.insert(bean);
            return "1";
        }catch (Exception e){
            System.out.println("添加info失败");
            return "0";
        }
    }
    //删除
    @RequestMapping("/del")
    public String infoDel(Integer id){
        try {
            infoMapper.deleteById(id);
            return "redirect:/vx/info";
        }catch (Exception e){
            System.out.println("删除求购信息失败");
            return "redirect:/vx/info";

        }
    }

}
